﻿Imports VendasDAL
Public Class UsuarioBLL
    Dim objDAL As UsuarioDAL
#Region "Propriedades"
    Property Codigo As Integer
    Property Descricao As String
    Property Status As Integer
    Property codfuncionario As Integer
    Property funcionario As String
    Property senha As Boolean

#End Region

#Region "Construtores"
    Sub New()

    End Sub
    Sub New(ByVal codigo As Integer)
        carrega(codigo)
    End Sub
    Sub New(codigo As Integer, descricao As String, status As Integer, funcionario As Integer, senha As Boolean)
        Me.Codigo = codigo
        Me.Descricao = descricao
        Me.Status = status
        Me.codfuncionario = funcionario
        Me.senha = senha
    End Sub

#End Region

#Region "Métodos"
#Region "Inserir"
    Function Inserir() As Boolean
        Try
            If (Descricao <> "" And codfuncionario <> 0) Then
                If (objfuncoes.ValidaReplica(Me.Descricao, Me.Codigo, "Vendas.Gen_usuario", "usu_codigo", "usu_login").Rows.Count = 0) Then
                    objDAL = New UsuarioDAL
                    Me.Codigo = objDAL.Inserir(Me.Descricao, Me.Status, Me.codfuncionario)
                Else : Throw New Exception("Usuário já cadastrado")
                End If
            Else : Throw New Exception("Favor preencher os campos obrigatórios")
            End If
        Catch ex As Exception
            Throw ex
        Finally
            ObjDAL = Nothing
        End Try
        If (Me.Codigo <= 0) Then
            Return False
        Else : Return True
        End If
    End Function

#End Region

#Region "Alterar"
    Function Alterar() As Boolean
        Dim ret As Integer
        Try
            ret = -1
            objDAL = New UsuarioDAL
            If (Descricao <> "" And codfuncionario > 0) Then
                If (objfuncoes.ValidaReplica(Me.Descricao, Me.Codigo, "Vendas.Gen_Usuario", "usu_codigo", "usu_login").Rows.Count = 0) Then
                    ret = objDAL.Alterar(Me.Descricao, Me.Status, Me.Codigo, Me.codfuncionario, Me.senha)
                Else : Throw New Exception("Usuário já cadastrado")
                End If
            Else : Throw New Exception("Favor preencher os campos obrigatórios")
            End If


        Catch ex As Exception
            Throw ex
        Finally
            ObjDAL = Nothing
        End Try
        If (ret <= 0) Then
            Return False
        Else : Return True
        End If
    End Function
#End Region

#Region "Excluir"
    Function Excluir() As Boolean
        Dim ret As Integer
        Try
            ret = -1
            objDAL = New UsuarioDAL
         

            ret = objDAL.Excluir(Me.Codigo, 3)
            'End if
        Catch ex As Exception
            Throw ex
        Finally
            objDAL = Nothing
        End Try
        If (ret <= 0) Then
            Return False
        Else : Return True
        End If
    End Function
#End Region

#Region "Buscar"
    Function buscar(ByVal descricao As String, tipo As Integer) As DataTable
        Dim ret As DataTable
        Try
            objDAL = New UsuarioDAL
            ret = objDAL.buscar(descricao, tipo)
        Catch ex As Exception
            Throw ex
        Finally
            objDAL = Nothing
        End Try
        Return ret
    End Function
#End Region

#Region "Valida Login"
    Public Function login(usuario As String, senha As String) As String
        Dim ret As String = ""
        Dim tab As New DataTable
        Try

            objDAL = New UsuarioDAL()
            tab = objDAL.login(usuario, senha)
            If (TAB.Rows.Count > 0) Then
                If (TAB.Rows(0)("usu_senha").ToString.Trim <> "") Then
                    If (tab.Rows(0)("usu_senha").ToString.Trim <> senha.Trim) Then
                        Throw New Exception("Senha inválida")
                    Else
                        If (Val(tab.Rows(0)("sta_codigo").ToString) = 1) Then
                            Me.carrega(Val(tab.Rows(0)("usu_codigo").ToString))
                            Return "OK"
                        Else : Throw New Exception("Usuário inativo")
                        End If
                    End If
                Else
                    Me.carrega(Val(tab.Rows(0)("usu_codigo").ToString))
                    Return "Primeiro Login"
                End If
            Else
                Throw New Exception("Usuário inválido")
            End If
        Catch ex As Exception
            Throw ex
        End Try
        Return ret
    End Function
#End Region

#Region "Alterar Senha"
    Function Alterarsenha(novasenha As String) As Boolean
        Dim ret As Integer
        Try
            ret = -1
            objDAL = New UsuarioDAL
            ret = objDAL.Alterarsenha(Me.Codigo, novasenha)
        Catch ex As Exception
            Throw ex
        Finally
            objDAL = Nothing
        End Try
        If (ret <= 0) Then
            Return False
        Else : Return True
        End If
    End Function
#End Region

#End Region

#Region "Carrega Propriedades"
    Sub carrega(ByVal codigo As Integer)
        Dim tab As DataTable
        Try
            objDAL = New UsuarioDAL

            tab = ObjDAL.buscar(codigo, 0)
            If (tab.Rows.Count > 0) Then
                Me.Codigo = Integer.Parse(tab.Rows(0)("usu_codigo").ToString)
                Me.Descricao = tab.Rows(0)("usu_login").ToString
                Me.Status = tab.Rows(0)("sta_codigo").ToString
                Me.funcionario = tab.Rows(0)("fun_nome").ToString
                Me.codfuncionario = tab.Rows(0)("fun_codigo").ToString
            End If
        Catch ex As Exception
            Throw ex
        Finally
            ObjDAL = Nothing
        End Try
    End Sub
#End Region

#Region "Limpa Propriedades"
    Sub limpar()
        Me.Codigo = Nothing
        Me.Descricao = Nothing
        Me.Status = Nothing
        Me.codfuncionario = Nothing
        Me.objDAL = Nothing
    End Sub
#End Region
End Class
